using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace ServiceStack
{
	public static class EnumerableExtensions
	{
		[CompilerGenerated]
		[Serializable]
		private sealed class <>c__10<T>
		{
			public static readonly EnumerableExtensions.<>c__10<T> <>9 = new EnumerableExtensions.<>c__10<T>();
			public static Func<T, T, bool> <>9__10_0;
			internal bool <EquivalentTo>b__10_0(T v1, T v2)
			{
				return v1.Equals(v2);
			}
		}
		[CompilerGenerated]
		[Serializable]
		private sealed class <>c__11<K, V>
		{
			public static readonly EnumerableExtensions.<>c__11<K, V> <>9 = new EnumerableExtensions.<>c__11<K, V>();
			public static Func<V, V, bool> <>9__11_0;
			internal bool <EquivalentTo>b__11_0(V v1, V v2)
			{
				return v1.Equals(v2);
			}
		}
		public static bool IsEmpty<T>(this ICollection<T> collection)
		{
			return collection == null || collection.Count == 0;
		}
		public static bool IsEmpty<T>(this T[] collection)
		{
			return collection == null || collection.Length == 0;
		}
		public static HashSet<T> ToHashSet<T>(this IEnumerable<T> items)
		{
			return new HashSet<T>(items);
		}
		public static void Each<T>(this IEnumerable<T> values, Action<T> action)
		{
			if (values == null)
			{
				return;
			}
			foreach (T current in values)
			{
				action(current);
			}
		}
		public static void Each<T>(this IEnumerable<T> values, Action<int, T> action)
		{
			if (values == null)
			{
				return;
			}
			int num = 0;
			foreach (T current in values)
			{
				action(num++, current);
			}
		}
		public static List<To> Map<To, From>(this IEnumerable<From> items, Func<From, To> converter)
		{
			if (items == null)
			{
				return new List<To>();
			}
			List<To> list = new List<To>();
			foreach (From current in items)
			{
				list.Add(converter(current));
			}
			return list;
		}
		public static List<To> Map<To>(this IEnumerable items, Func<object, To> converter)
		{
			if (items == null)
			{
				return new List<To>();
			}
			List<To> list = new List<To>();
			foreach (object current in items)
			{
				list.Add(converter(current));
			}
			return list;
		}
		public static List<object> ToObjects<T>(this IEnumerable<T> items)
		{
			List<object> list = new List<object>();
			foreach (T current in items)
			{
				list.Add(current);
			}
			return list;
		}
		public static string FirstNonDefaultOrEmpty(this IEnumerable<string> values)
		{
			foreach (string current in values)
			{
				if (!string.IsNullOrEmpty(current))
				{
					return current;
				}
			}
			return null;
		}
		public static T FirstNonDefault<T>(this IEnumerable<T> values)
		{
			foreach (T current in values)
			{
				object arg_24_0 = current;
				T t = default(T);
				if (!object.Equals(arg_24_0, t))
				{
					t = current;
					return t;
				}
			}
			return default(T);
		}
		public static bool EquivalentTo<T>(this IEnumerable<T> thisList, IEnumerable<T> otherList, Func<T, T, bool> comparer = null)
		{
			if (comparer == null)
			{
				Func<T, T, bool> arg_22_0;
				if ((arg_22_0 = EnumerableExtensions.<>c__10<T>.<>9__10_0) == null)
				{
					arg_22_0 = (EnumerableExtensions.<>c__10<T>.<>9__10_0 = new Func<T, T, bool>(EnumerableExtensions.<>c__10<T>.<>9.<EquivalentTo>b__10_0));
				}
				comparer = arg_22_0;
			}
			if (thisList == null || otherList == null)
			{
				return thisList == otherList;
			}
			IEnumerator<T> enumerator = otherList.GetEnumerator();
			foreach (T current in thisList)
			{
				if (!enumerator.MoveNext())
				{
					bool result = false;
					return result;
				}
				bool flag = object.Equals(current, default(T));
				bool flag2 = object.Equals(enumerator.Current, default(T));
				if (flag | flag2)
				{
					bool result = flag & flag2;
					return result;
				}
				if (!comparer(current, enumerator.Current))
				{
					bool result = false;
					return result;
				}
			}
			return !enumerator.MoveNext();
		}
		public static bool EquivalentTo<K, V>(this IDictionary<K, V> a, IDictionary<K, V> b, Func<V, V, bool> comparer = null)
		{
			if (comparer == null)
			{
				Func<V, V, bool> arg_22_0;
				if ((arg_22_0 = EnumerableExtensions.<>c__11<K, V>.<>9__11_0) == null)
				{
					arg_22_0 = (EnumerableExtensions.<>c__11<K, V>.<>9__11_0 = new Func<V, V, bool>(EnumerableExtensions.<>c__11<K, V>.<>9.<EquivalentTo>b__11_0));
				}
				comparer = arg_22_0;
			}
			if (a == null || b == null)
			{
				return a == b;
			}
			if (a.Count != b.Count)
			{
				return false;
			}
			foreach (KeyValuePair<K, V> current in a)
			{
				V v;
				if (!b.TryGetValue(current.Key, out v))
				{
					bool result = false;
					return result;
				}
				if (current.Value == null || v == null)
				{
					if (current.Value != null || v != null)
					{
						bool result = false;
						return result;
					}
				}
				else
				{
					if (!comparer(current.Value, v))
					{
						bool result = false;
						return result;
					}
				}
			}
			return true;
		}
		public static IEnumerable<T[]> BatchesOf<T>(this IEnumerable<T> sequence, int batchSize)
		{
			List<T> list;
			IEnumerator<T> enumerator;
			try
			{
				list = new List<T>(batchSize);
				enumerator = sequence.GetEnumerator();
				while (enumerator.MoveNext())
				{
					T current = enumerator.Current;
					list.Add(current);
					if (list.Count >= batchSize)
					{
						yield return list.ToArray();
						list.Clear();
					}
				}
			}
			finally
			{
				if (enumerator != null)
				{
					enumerator.Dispose();
				}
			}
			enumerator = null;
			if (list.Count > 0)
			{
				yield return list.ToArray();
				list.Clear();
			}
			yield break;
			yield break;
		}
		public static Dictionary<TKey, T> ToSafeDictionary<T, TKey>(this IEnumerable<T> list, Func<T, TKey> expr)
		{
			Dictionary<TKey, T> dictionary = new Dictionary<TKey, T>();
			if (list != null)
			{
				foreach (T current in list)
				{
					dictionary[expr(current)] = current;
				}
			}
			return dictionary;
		}
		public static Dictionary<TKey, TValue> ToDictionary<T, TKey, TValue>(this IEnumerable<T> list, Func<T, KeyValuePair<TKey, TValue>> map)
		{
			Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>();
			foreach (T current in list)
			{
				KeyValuePair<TKey, TValue> keyValuePair = map(current);
				dictionary[keyValuePair.Key] = keyValuePair.Value;
			}
			return dictionary;
		}
		public static IEnumerable<T> Safe<T>(this IEnumerable<T> enumerable)
		{
			return enumerable ?? new T[0];
		}
	}
}
